package com.zhuhanpeng.cloud.user.control;

import com.alibaba.fastjson.JSON;
import com.zhuhanpeng.cloud.constant.CloudConstant;
import com.zhuhanpeng.cloud.exception.EmailExitException;
import com.zhuhanpeng.cloud.exception.MobileExitException;
import com.zhuhanpeng.cloud.exception.UserNameExitException;
import com.zhuhanpeng.cloud.mybatis.entity.PtUserEntity;
import com.zhuhanpeng.cloud.mybatis.flexigrid.PadingRstType;
import com.zhuhanpeng.cloud.mybatis.flexigrid.PagingEntity;
import com.zhuhanpeng.cloud.mybatis.ztree.ZtreeEntity;
import com.zhuhanpeng.cloud.spring.control.BaseController;
import com.zhuhanpeng.cloud.user.entity.SearchUserEntity;
import com.zhuhanpeng.cloud.user.entity.UserEntity;
import com.zhuhanpeng.cloud.user.exception.PasswordError;
import com.zhuhanpeng.cloud.user.service.UserService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController extends BaseController {
    private Logger logger = LogManager.getLogger(this.getClass());
    @Resource
    private UserService userService;
    @Secured("ROLE_cldk_data_user")
    @RequestMapping("/userPage")
    public String userPage(){
        return "user/userPage";
    }


    @RequestMapping("/modifyPassword")
    @ResponseBody
    public String modifyPassword(@RequestParam("oldPassword")String oldPassword, @RequestParam("newPassword") String newPassword){
        logger.info("oldPassword:" + oldPassword + "newPassword:" + newPassword);
//        userService.modifyPassword(oldPassword,newPassword);
        try {
            userService.modifyPassword(oldPassword,newPassword);
        } catch (PasswordError passwordError) {
            //passWordError.printStackTrace();
            return failCodeJson("main.modify.password.fail", CloudConstant.OLD_PASSWORD_EXIT_ERROR);
        }
        return sucessJson("main.modify.password.success");
    }
    @RequestMapping("/queryUserInfoListByPage")
    @ResponseBody
    public String queryUserInfoListByPage(SearchUserEntity entity, PagingEntity paging){
        logger.info(paging);
        PadingRstType<PtUserEntity> ptUserEntityPadingRstType = userService.queryUserInfoListByPage(entity,paging);
        return JSON.toJSONString(ptUserEntityPadingRstType);
    }
    @RequestMapping("/addUser")
    @ResponseBody
    public String addUser(UserEntity entity){
        logger.info(entity);
//        userService.addUser(entity);
        try {
            userService.addUser(entity);
        } catch (UserNameExitException e) {
            e.printStackTrace();
            return failCodeJson("user.add.fail", CloudConstant.USER_NAME_EXIT_ERROR);
        } catch (EmailExitException e) {
            e.printStackTrace();
            return failCodeJson("user.add.fail", CloudConstant.USER_EMAIL_EXIT_ERROR);
        } catch (MobileExitException e) {
            e.printStackTrace();
            return failCodeJson("user.add.fail", CloudConstant.USER_MOBILE_EXIT_ERROR);
        }
        return sucessJson("user.add.success");
    }
    @RequestMapping("/queryUserInfoById")
    @ResponseBody
    public UserEntity queryUserInfoById(@RequestParam("userUuid") String userUuid){
        logger.info("userUuid" + userUuid);
        UserEntity entity= userService.queryUserInfoById(userUuid);
        return entity;
    }
    @RequestMapping("/modifyUser")
    @ResponseBody
    public String modifyUser(UserEntity entity){
        logger.info(entity);
//        userService.modifyUser(entity);
        try {
            userService.modifyUser(entity);
        } catch (UserNameExitException e) {
            e.printStackTrace();
            return failCodeJson("user.mod.fail", CloudConstant.USER_NAME_EXIT_ERROR);
        } catch (EmailExitException e) {
            e.printStackTrace();
            return failCodeJson("user.mod.fail", CloudConstant.USER_NAME_EXIT_ERROR);
        } catch (MobileExitException e) {
            e.printStackTrace();
            return failCodeJson("user.mod.fail", CloudConstant.USER_NAME_EXIT_ERROR);
        }
        return sucessJson("user.mod.success");
    }

    @RequestMapping("/deleteUserInfoByIds")
    @ResponseBody
    public String deleteUserInfoByIds(@RequestParam("userUuids") String userUuids){
        logger.info("userUuids:" + userUuids);
        userService.deleteUserInfoByIds(userUuids);
        return sucessJson("user.del.user.success");
    }
    @RequestMapping("/queryRoleZtreeByUserId")
    @ResponseBody
    public List<ZtreeEntity> queryRoleZtreeByUserId(@RequestParam("userUuid") String userUuid ){
        List<ZtreeEntity> list = userService.queryRoleZtreeByUserId(userUuid);
        return list;
    }
    @RequestMapping("/saveRoleRefUser")
    @ResponseBody
    public String saveRoleRefUser(@RequestParam("userUuidArray") String userUuidArray,@RequestParam("rolesArray") String rolesArray){
        logger.info("userUuidArray:" + userUuidArray + "\r\nrolesArray:" + rolesArray);
        userService.saveRoleRefUser(userUuidArray,rolesArray);
        return sucessJson("user.assign.role.success");
    }
    @RequestMapping("/queryRoleZtreeAll")
    @ResponseBody
    public List<ZtreeEntity> queryRoleZtreeAll(){
        List<ZtreeEntity> list = userService.queryRoleZtreeAll();
        return list;
    }
    @RequestMapping("/resetPassword")
    @ResponseBody
    public String resetPassword(@RequestParam("userUuidArray") String userUuidArray,@RequestParam("password") String password){
        logger.info("userUuidArray:"+userUuidArray);
        userService.resetPassword(userUuidArray,password);
        return sucessJson("user.reset.password.success");

    }
}
